package com.wbao.dianniu.tabstrip.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener2;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.R;
import com.wbao.dianniu.adapter.AnonymousAdapter;
import com.wbao.dianniu.data.QaResponse;
import com.wbao.dianniu.listener.IQaListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.update.AmDataChangeManager;
import com.wbao.dianniu.update.AmDataChangeManager.IAmDataListener;
import com.wbao.dianniu.manager.QaManager;
import com.wbao.dianniu.utils.Utils;

import java.util.List;

/**
 * 匿名八卦
 *
 * @author yejianjun
 */
public class AmFragment extends Fragment implements IQaListener, IAmDataListener {
    private LinearLayout waittingLayout;
    private ProgressBar waittingProBar;
    private TextView waittingTV;

    private final int TYPE_ANONYMOUS = 2;
    private PullToRefreshListView mListView;
    //是否是下拉刷新
    private boolean isPullDown = true;
    private AnonymousAdapter adapter;
    private QaManager manager;
    private final int PAGE_COUNT = 10;
    private AmDataChangeManager amDataChangeManager;

    public static AmFragment instance() {
        AmFragment view = new AmFragment();
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new AnonymousAdapter(getActivity(), 2);
        amDataChangeManager = AmDataChangeManager.getInstance();
        amDataChangeManager.addAmChangeListener(this);
    }

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_fist_pager_anonymous, null);
        init(view);
        initWattingView(view);
        requestData();
        return view;
    }

    private void init(View view) {
        mListView = (PullToRefreshListView) view.findViewById(R.id.anonmous_listview);
        mListView.setMode(Mode.BOTH);
        mListView.setOnRefreshListener(onRefreshListener);

        ListView actualListView = mListView.getRefreshableView();
        registerForContextMenu(actualListView);
        actualListView.setAdapter(adapter);
        actualListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                Utils.intoAmDetailActivity(getContext(), (QaResponse) adapter.getItem(position - 1));
            }
        });
    }

    private void initWattingView(View view) {
        waittingLayout = (LinearLayout) view.findViewById(R.id.waitting_layout);
        waittingProBar = (ProgressBar) view.findViewById(R.id.waitting_progress);
        waittingTV = (TextView) view.findViewById(R.id.waitting_text_desc);
    }

    private void requestData() {
        manager = new QaManager(getActivity());
        manager.addQaListener(this);
        reqData("down");
    }

    private void reqData(String refresh) {
        if (isPullDown) {
            manager.reqData(GlobalContext.getAccountId(), null, TYPE_ANONYMOUS, 0, PAGE_COUNT, adapter.getFirstId(), refresh);
        } else {
            manager.reqData(GlobalContext.getAccountId(), null, TYPE_ANONYMOUS, 0, PAGE_COUNT, adapter.getLastId(), refresh);
        }
    }

    OnRefreshListener2 onRefreshListener = new OnRefreshListener2() {
        // 下拉Pulling Down
        @Override
        public void onPullDownToRefresh() {
            //请求最新的数据
            isPullDown = true;
            reqData("down");
        }

        @Override
        public void onPullUpToRefresh() {
            //请求下一页数据
            isPullDown = false;
            reqData("up");
        }
    };

    @Override
    public void onQaFailure(int errorCode, String errorMsg) {
        // TODO Auto-generated method stub
        waittingLayout.setVisibility(View.GONE);
        mListView.onRefreshComplete(false, false);
    }

    @Override
    public void onQaSuccess(List<QaResponse> list) {
        waittingLayout.setVisibility(View.GONE);
        mListView.onRefreshComplete(false, false);
        if (null != list) {
            if (isPullDown) {
                adapter.addForwardList(list);
            } else {
                adapter.addBackwardList(list);
            }
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.removeQaListener(this);
        }
        if(null != amDataChangeManager)
            amDataChangeManager.removeAmChangeListener(this);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("AmFragment"); //统计页面，"MainScreen"为页面名称，可自定义
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("AmFragment");
    }

    @Override
    public void addOneData(QaResponse addData) {
        // TODO Auto-generated method stub
        adapter.addOneQuestData(addData);
    }

    @Override
    public void removeOneData(int questId) {
        // TODO Auto-generated method stub
        adapter.removeOneData(questId);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void priseNotify(int questId, int isGood, int goodCounts) {
        // TODO Auto-generated method stub
        adapter.priseNotify(questId, isGood, goodCounts);
    }

    @Override
    public void replyNotify(int questId, int answerCounts) {
        // TODO Auto-generated method stub
        adapter.replyNotify(questId, answerCounts);
    }

    @Override
    public void updateVisitor(int questId, int visitorCounts) {
        adapter.updateVisitor(questId, visitorCounts);
    }
}
